/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ProductShoppingCartPane.java
 *
 * Created on 25-jul-2010, 10:25:59
 */

package facturasapp;

import facturasapp.data.DataManager;
import facturasapp.events.FacturaEvent;
import facturasapp.events.EventManager;
import storeDataModel.Invoice;
import storeDataModel.ProductRelation;

/**
 *
 * @author CiberXtrem
 */
public class ProductShoppingCartPane extends javax.swing.JPanel {

    /** Creates new form ProductShoppingCartPane */
    public ProductShoppingCartPane(ProductRelation p_ProductRelation) {
        productRelation = p_ProductRelation;
        initComponents();
        initCustomComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cantidadLabel = new javax.swing.JLabel();
        precioField = new javax.swing.JTextField();
        precioLabel = new javax.swing.JLabel();
        cantidadField = new javax.swing.JTextField();
        descripcionLabel = new javax.swing.JLabel();
        importeLabel = new javax.swing.JLabel();
        descripcionField = new javax.swing.JTextField();
        importeField = new javax.swing.JTextField();
        codigoLabel = new javax.swing.JLabel();
        addProductoCestaButton = new javax.swing.JButton();
        codigoField = new javax.swing.JTextField();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(facturasapp.FacturasApp.class).getContext().getResourceMap(ProductShoppingCartPane.class);
        cantidadLabel.setFont(resourceMap.getFont("precioLabel.font")); // NOI18N
        cantidadLabel.setForeground(resourceMap.getColor("precioLabel.foreground")); // NOI18N
        cantidadLabel.setText(resourceMap.getString("cantidadLabel.text")); // NOI18N
        cantidadLabel.setName("cantidadLabel"); // NOI18N

        precioField.setFont(resourceMap.getFont("cantidadField.font")); // NOI18N
        precioField.setForeground(resourceMap.getColor("cantidadField.foreground")); // NOI18N
        precioField.setText(resourceMap.getString("precioField.text")); // NOI18N
        precioField.setName("precioField"); // NOI18N
        precioField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                precioFieldFocusLost(evt);
            }
        });

        precioLabel.setFont(resourceMap.getFont("precioLabel.font")); // NOI18N
        precioLabel.setForeground(resourceMap.getColor("precioLabel.foreground")); // NOI18N
        precioLabel.setText(resourceMap.getString("precioLabel.text")); // NOI18N
        precioLabel.setName("precioLabel"); // NOI18N

        cantidadField.setFont(resourceMap.getFont("cantidadField.font")); // NOI18N
        cantidadField.setForeground(resourceMap.getColor("cantidadField.foreground")); // NOI18N
        cantidadField.setText(resourceMap.getString("cantidadField.text")); // NOI18N
        cantidadField.setName("cantidadField"); // NOI18N
        cantidadField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                cantidadFieldFocusLost(evt);
            }
        });

        descripcionLabel.setFont(resourceMap.getFont("precioLabel.font")); // NOI18N
        descripcionLabel.setForeground(resourceMap.getColor("precioLabel.foreground")); // NOI18N
        descripcionLabel.setText(resourceMap.getString("descripcionLabel.text")); // NOI18N
        descripcionLabel.setName("descripcionLabel"); // NOI18N

        importeLabel.setFont(resourceMap.getFont("precioLabel.font")); // NOI18N
        importeLabel.setForeground(resourceMap.getColor("precioLabel.foreground")); // NOI18N
        importeLabel.setText(resourceMap.getString("importeLabel.text")); // NOI18N
        importeLabel.setName("importeLabel"); // NOI18N

        descripcionField.setFont(resourceMap.getFont("cantidadField.font")); // NOI18N
        descripcionField.setForeground(resourceMap.getColor("cantidadField.foreground")); // NOI18N
        descripcionField.setText(resourceMap.getString("descripcionField.text")); // NOI18N
        descripcionField.setName("descripcionField"); // NOI18N
        descripcionField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                descripcionFieldFocusLost(evt);
            }
        });

        importeField.setFont(resourceMap.getFont("cantidadField.font")); // NOI18N
        importeField.setForeground(resourceMap.getColor("cantidadField.foreground")); // NOI18N
        importeField.setText(resourceMap.getString("importeField.text")); // NOI18N
        importeField.setName("importeField"); // NOI18N
        importeField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                importeFieldFocusLost(evt);
            }
        });

        codigoLabel.setFont(resourceMap.getFont("precioLabel.font")); // NOI18N
        codigoLabel.setForeground(resourceMap.getColor("precioLabel.foreground")); // NOI18N
        codigoLabel.setText(resourceMap.getString("codigoLabel.text")); // NOI18N
        codigoLabel.setName("codigoLabel"); // NOI18N

        addProductoCestaButton.setIcon(resourceMap.getIcon("addProductoCestaButton.icon")); // NOI18N
        addProductoCestaButton.setName("addProductoCestaButton"); // NOI18N
        addProductoCestaButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addProductoCestaButtonActionPerformed(evt);
            }
        });

        codigoField.setFont(resourceMap.getFont("cantidadField.font")); // NOI18N
        codigoField.setForeground(resourceMap.getColor("cantidadField.foreground")); // NOI18N
        codigoField.setText(resourceMap.getString("codigoField.text")); // NOI18N
        codigoField.setName("codigoField"); // NOI18N
        codigoField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                codigoFieldFocusLost(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(codigoField, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(codigoLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(descripcionLabel)
                    .addComponent(descripcionField, javax.swing.GroupLayout.PREFERRED_SIZE, 225, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(precioLabel)
                    .addComponent(precioField, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cantidadField, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cantidadLabel))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(importeLabel)
                    .addComponent(importeField, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(addProductoCestaButton, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(addProductoCestaButton)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(importeLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE)
                            .addComponent(cantidadLabel))
                        .addGap(20, 20, 20))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(codigoLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(codigoField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(descripcionLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(descripcionField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(precioLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(precioField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(cantidadField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(importeField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

 public void initCustomComponents(){
 }

 public void updateImporte(){
    float precio = Float.parseFloat(precioField.getText());
    int cantidad = Integer.parseInt(cantidadField.getText());
    float importe = precio * (float) cantidad;

    importeField.setText(String.valueOf(importe));
}

    private void addProductoCestaButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addProductoCestaButtonActionPerformed
        //Data Model
        invoice = (Invoice) DataManager.getInstance().getDataObject("currentInvoice");
        invoice.removeProductRelation(productRelation);

        //Send Event to update Visual part
        FacturaEvent event = new FacturaEvent(this);
        EventManager.getInstance().fireFacturaEvent(event , EventManager.INVOICE_PRODUCT_REMOVED);

        //Lanzamos evento para indicar que la factura se ha actualizado
        FacturaEvent ev = new FacturaEvent(this); //No es necesario
        EventManager.getInstance().fireFacturaEvent(ev, EventManager.INVOICE_UPDATED);
}//GEN-LAST:event_addProductoCestaButtonActionPerformed

    private void codigoFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_codigoFieldFocusLost
        String codigo = codigoField.getText();
        productRelation.getProducto().setCodigo(codigo);

    }//GEN-LAST:event_codigoFieldFocusLost

    private void descripcionFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_descripcionFieldFocusLost
        String descripcion = descripcionField.getText();
        //productRelation.getProducto().setDescripcion(descripcion);
        productRelation.setDescripcion(descripcion);
    }//GEN-LAST:event_descripcionFieldFocusLost

    private void precioFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_precioFieldFocusLost
        updateImporte();
        float precio = Float.valueOf(precioField.getText());
        //productRelation.getProducto().setPrecio(precio);
        productRelation.setPrecio(precio);
        
        float importe = Float.valueOf(importeField.getText());
        productRelation.setImporte(importe);

        //Lanzamos evento para indicar que la factura se ha actualizado
        FacturaEvent ev = new FacturaEvent(this); //No es necesario
        EventManager.getInstance().fireFacturaEvent(ev, EventManager.INVOICE_UPDATED);
    }//GEN-LAST:event_precioFieldFocusLost

    private void cantidadFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_cantidadFieldFocusLost
        updateImporte();
        int cantidad = Integer.valueOf(cantidadField.getText());
        productRelation.setCantidad(cantidad);

        float importe = Float.valueOf(importeField.getText());
        productRelation.setImporte(importe);

        //Lanzamos evento para indicar que la factura se ha actualizado
        FacturaEvent ev = new FacturaEvent(this); //No es necesario
        EventManager.getInstance().fireFacturaEvent(ev, EventManager.INVOICE_UPDATED);
    }//GEN-LAST:event_cantidadFieldFocusLost

    private void importeFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_importeFieldFocusLost
        float importe = Float.valueOf(importeField.getText());
        productRelation.setImporte(importe);

        //Lanzamos evento para indicar que la factura se ha actualizado
        FacturaEvent ev = new FacturaEvent(this); //No es necesario
        EventManager.getInstance().fireFacturaEvent(ev, EventManager.INVOICE_UPDATED);
    }//GEN-LAST:event_importeFieldFocusLost


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addProductoCestaButton;
    public javax.swing.JTextField cantidadField;
    private javax.swing.JLabel cantidadLabel;
    public javax.swing.JTextField codigoField;
    private javax.swing.JLabel codigoLabel;
    public javax.swing.JTextField descripcionField;
    private javax.swing.JLabel descripcionLabel;
    public javax.swing.JTextField importeField;
    private javax.swing.JLabel importeLabel;
    public javax.swing.JTextField precioField;
    private javax.swing.JLabel precioLabel;
    // End of variables declaration//GEN-END:variables

    public ProductRelation productRelation=null;
    Invoice invoice = null;

}
